﻿Public Class frmQuanLyThongTinThueNha

    Private Sub frmQuanLyThongTinThueNha_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim listTTThueNha As New DataTable()
        listTTThueNha = TT_ThueNhaBUS.GetAll()
        gvDSThue.DataSource = listTTThueNha


        Dim listKhachThue As New List(Of KhachThueDTO)
        listKhachThue = KhachThueBUS.GetAll()

        cbxMaKH.DataSource = listKhachThue
        cbxMaKH.ValueMember = "strMaKhachThue"
        cbxMaKH.DisplayMember = "strHoTen"

        Dim listNha As New DataTable
        listNha = NhaBUS.GetAll()
        cbxMaNha.DataSource = listNha
        cbxMaNha.DisplayMember = "MaNha"

        Dim listQuan As New List(Of QuanDTO)
        listQuan = QuanBUS.GetAll()
        cbxTimQuan.DataSource = listQuan
        cbxTimQuan.ValueMember = "iMaQuan"
        cbxTimQuan.DisplayMember = "strTenQuan"

        btnLuuLai.Enabled = False
    End Sub

    Private Sub gvDSThue_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvDSThue.SelectionChanged
        cbxMaKH.Text = gvDSThue.CurrentRow.Cells(1).Value
        cbxMaNha.Text = gvDSThue.CurrentRow.Cells(3).Value
        txtGia.Text = String.Format("{0:0,0}", gvDSThue.CurrentRow.Cells(5).Value)
        dtNgayThue.Text = gvDSThue.CurrentRow.Cells(6).Value
    End Sub

    
    Private Sub btnLuuLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuuLai.Click
        Dim thongtinTN As New TT_ThueNhaDTO()

     
        If TT_ThueNhaBUS.KiemTraDayDu(txtGia.Text) Then
            thongtinTN.strMaKhachThue = cbxMaKH.SelectedValue
            thongtinTN.strMaNha = cbxMaNha.Text
            thongtinTN.dNgayThue = dtNgayThue.Value
            thongtinTN.iGia = txtGia.Text

            TT_ThueNhaBUS.Insert(thongtinTN)

            MessageBox.Show("Thông tin đã Thêm  thành công!", "THÔNG BÁO")

            Dim listTTThueNha As New DataTable()
            listTTThueNha = TT_ThueNhaBUS.GetAll()
            gvDSThue.DataSource = listTTThueNha

        Else
            MessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "THÔNG BÁO")
        End If

        btnLuuLai.Enabled = False
        btnSuaThongTin.Enabled = True
        btnXoaThongTin.Enabled = True
    End Sub

    Private Sub btnThemThongTin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemThongTin.Click
        cbxMaKH.SelectedIndex = 0
        cbxMaNha.SelectedIndex = 0
        txtGia.Text = ""
        dtNgayThue.Value = Date.Now.Date

        btnLuuLai.Enabled = True
        btnSuaThongTin.Enabled = False
        btnXoaThongTin.Enabled = False
    End Sub

    Private Sub btnXoaThongTin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaThongTin.Click
        If IsNothing(gvDSThue.SelectedRows) Then
            MessageBox.Show("Bạn chưa chọn dòng nào để Xóa!!", "THÔNG BÁO")
        Else
            If MessageBox.Show("Bạn có chắc chắn Xóa không?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
                Dim idMaTT As Integer
                idMaTT = gvDSThue.CurrentRow.Cells("MaTT").Value

                TT_ThueNhaBUS.delete_TTThue(idMaTT)
                MessageBox.Show("Dữ liệu đã được Xóa!!", "THÔNG BÁO")

                Dim listTTThueNha As New DataTable()
                listTTThueNha = TT_ThueNhaBUS.GetAll()
                gvDSThue.DataSource = listTTThueNha
               
            End If
        End If
    End Sub

    Private Sub btnSuaThongTin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuaThongTin.Click
        Dim thongtinTN As New TT_ThueNhaDTO()


        If TT_ThueNhaBUS.KiemTraDayDu(txtGia.Text) Then
            thongtinTN.strMaKhachThue = cbxMaKH.SelectedValue
            thongtinTN.strMaNha = cbxMaNha.Text
            thongtinTN.dNgayThue = dtNgayThue.Value
            thongtinTN.iGia = txtGia.Text

            TT_ThueNhaBUS.Update(thongtinTN)

            MessageBox.Show("Bạn đã Sửa thành công !!", "THÔNG BÁO")


            Dim listTTThueNha As New DataTable()
            listTTThueNha = TT_ThueNhaBUS.GetAll()
            gvDSThue.DataSource = listTTThueNha
        Else
            MessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "THÔNG BÁO")
        End If
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click

        Dim luachon As Integer = cbxLuaChon.SelectedIndex

        Select Case luachon
            Case 0
                Dim listTTThueNha As New DataTable()
                listTTThueNha = TT_ThueNhaBUS.TimKiemTTTheoQuan(cbxTimQuan.SelectedValue)
                    If listTTThueNha.Rows.Count = 0 Then
                        MessageBox.Show("Xin lỗi! Không tìm thấy thông tin cần tìm !!", "THÔNG BÁO")
                    Else
                        gvDSThue.DataSource = listTTThueNha

                    End If
            Case 1
                If Not txtTimGia.Text = "" Then
                    Dim gia As String = txtTimGia.Text
                    Dim listTTThueNha As New DataTable()
                    listTTThueNha = TT_ThueNhaBUS.TimKiemTTTheoGia(txtTimGia.Text)
                    If listTTThueNha.Rows.Count = 0 Then
                        MessageBox.Show("Xin lỗi! Không tìm thấy thông tin cần tìm !!", "THÔNG BÁO")
                    Else
                        gvDSThue.DataSource = listTTThueNha

                    End If

                Else
                    MessageBox.Show("Bạn Chưa nhập thông tin để Tìm!!", "THÔNG BÁO")
                End If
            Case 2
                If Not txtTimGia.Text = "" Then
                    Dim gia As String = txtTimGia.Text
                    Dim listTTThueNha As New DataTable()
                    listTTThueNha = TT_ThueNhaBUS.TimKiemTTTheoGia_Quan(cbxTimQuan.SelectedValue, txtTimGia.Text)
                    If listTTThueNha.Rows.Count = 0 Then
                        MessageBox.Show("Xin lỗi! Không tìm thấy thông tin cần tìm !!", "THÔNG BÁO")
                    Else
                        gvDSThue.DataSource = listTTThueNha

                    End If

                Else
                    MessageBox.Show("Bạn Chưa nhập thông tin để Tìm!!", "THÔNG BÁO")
                End If
                End Select
    End Sub

    Private Sub cbxLuaChon_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxLuaChon.SelectedIndexChanged
        Dim luachon As Integer = cbxLuaChon.SelectedIndex

        Select Case luachon
            Case 0
                lbQuan.Enabled = True
                cbxTimQuan.Enabled = True

                lbGia.Enabled = False
                txtTimGia.Enabled = False
            Case 1
                lbGia.Enabled = True
                txtTimGia.Enabled = True

                lbQuan.Enabled = False
                cbxTimQuan.Enabled = False
            Case 2
                lbGia.Enabled = True
                txtTimGia.Enabled = True

                lbQuan.Enabled = True
                cbxTimQuan.Enabled = True

        End Select
    End Sub

    Private Sub btnTatCa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTatCa.Click
        Dim listTTThueNha As New DataTable()
        listTTThueNha = TT_ThueNhaBUS.GetAll()
        gvDSThue.DataSource = listTTThueNha
    End Sub

    Private Sub gvDSThue_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles gvDSThue.CellFormatting
        If gvDSThue.Columns(e.ColumnIndex).Name = "Gia" Then
            e.CellStyle.Format = "#,##"
        End If
    End Sub
End Class